<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class user extends CI_Controller {
	
    public function register()
    {
    	$data = array();
    	$data['usernameExist']="";
   		$session = $this->session->all_userdata();
    	if(!isset($session['username'])){
    		$data['controller']='';
		   	$data['name_view']='home';
    		$this->load->view('Aquasprite',$data);
    		return;
    	}
    	if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    		$fname=$_POST['fname'];
            $lname = $_POST['lname'];
            $username=$_POST['userName'];
            $password = sha1($_POST ['password']);
            $email = $_POST['email']; 
            $address = $_POST['address'];
            $this->load->model('user_model');
            $user= $this->user_model->getAllOfUser();   
			if($user!=false){
	            foreach ($user as $row){
	            	if($row->user_name==$username){
	            		$data['controller']='user/';
				    	$data['name_view']='register';
				    	$data['usernameExist']='This username already Exist';
				    	$this->load->view('Aquasprite',$data);
	            		return;
	            	}
	            }
			}
			if(isset($fname)&&isset($lname)&&isset($username)&&isset($password)&&isset($email)&&isset($address)){
	            $userData = array(
					'role_id'=> 2,
					'password'=> $password,
					'user_name'=> $username,
					'first_name'=> $fname,
					'last_name' => $lname,
					'email'=> $email,
					'address'=> $address,
	            	'status'=>0,
				);
			}
			$insert=$this->db->insert('users', $userData);
            if($insert){
           		$data['fname']= $fname; 
           		$data['lname']= $lname;
		        $this->load->view('user/successfullForm',$data); 
		        return;
            }
 			
    	}
    	$data['controller']='user/';
    	$data['name_view']='register';
    	$this->load->view('Aquasprite',$data);
       // $this->load->view('template',$data);
    }
    public function login(){
    	$data = array();
    	$this->load->library('session');
    	$this->load->model('user_model');
    	$data['controller']='user/';
    	$data['name_view']='login';
    	$data['succes']="";
    	if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    		$userName=$_POST['userName'];
    		$password=sha1($_POST['password']);
    		$user = $this->user_model->getUserByUserName($userName);
			if($user==false){
				$data['succes']="Login unsuccessfully";
			}elseif ($user['password']!=$password){
				$data['succes']="Login unsuccessfully";
    		}else{
    			$userArray = array(
                   'username'  => $userName,
                   'password'     => $password,
            	);
            	$data['userName']=$userName;
				$data['controller']='';
    			$data['name_view']='home';	
    			$this->session->set_userdata($userArray);
			}  
    	}		
    	$this->load->view('Aquasprite',$data);
    }
    public function logout(){
    	$this->session->sess_destroy();
    	$data['controller']='';
    	$data['name_view']='home';	
    	$this->load->view('Aquasprite',$data);
    }
    
    public function update(){
    	$session = $this->session->all_userdata();
    	$data = array();
    	if(!isset($session['username'])){
    		$data['controller']='';
		   	$data['name_view']='home';
    		$this->load->view('Aquasprite',$data);
    		return;
    	}
    	 $data['success']="";
    	if ($_SERVER['REQUEST_METHOD'] == 'GET'){
    		$userName= $session['username'];
    		$this->load->model('user_model');
    		$user = $this->user_model->getUserByUserName($userName); 
    		$data['user']=$user;
    		$data['controller']='user/';
    		$data['name_view']='update';
    		$this->load->view('Aquasprite',$data);

    	}elseif($_SERVER['REQUEST_METHOD'] == 'POST'){
    		$fname=$_POST['fname'];
            $lname = $_POST['lname'];
            $userName=$_POST['userName'];
            $password = sha1($_POST ['password']);
            $email = $_POST['email']; 
            $address = $_POST['address'];
            $this->load->model('user_model');
            $userData = array(
					'role_id'=> 2,
					'password'=> $password,
					'first_name'=> $fname,
					'last_name' => $lname,
					'email'=> $email,
					'address'=> $address,
				);
            
    		 $this->user_model->updateUser($userName,$userData);
    	    $data['success']="Update successfully";
    	
    	    $user = $this->user_model->getUserByUserName($userName); 
    	    $data['user']=$user;
    	    $data['controller']='user/';
    		$data['name_view']='update';
    		$this->load->view('Aquasprite',$data);
    	}
    }
}

?>